<?php
include "../lib/DAL/DmTrinhDo.php";

class DmTrinhDoBLL{
    function __construct(){
    }
    
    function __destruct(){
    }

    public function SetDmTrinhDoBLL($row)
    {
        $trinhdo= new DmTrinhDo();
        $trinhdo->setDmTrinhDo($row['madm_td'],$row['ten_td'],$row['ten_vt_td']);
        return $trinhdo;   
    }
    
    public function GetlistTrinhDo(){
        $ex=mysql_query("SELECT * FROM dm_trinhdo");
        $ds= array();
        if($ex){
            while($row=mysql_fetch_array($ex)){
                $ds[]=$this->SetDmTrinhDoBLL($row);
            }
        }
     return $ds;
    }

    public function InsertTrinhDo($dt)
    {
        $sql="  INSERT INTO dm_trinhdo(madm_td,ten_td,ten_vt_td) 
                VALUES('".$dt->getmadm_td()."','".$dt->getten_td()."','".$dt->getten_vt_td()."')";
        
        $kq= mysql_query($sql);
        
        return $kq;
    }
    
    public function UpdateTrinhDo($dt){
        $sql="  UPDATE dm_trinhdo 
                SET  ten_td='".$dt->getten_td()."', ten_vt_td='".$dt->getten_vt_td()."'
                WHERE madm_td='".$dt->getmadm_td()."'";
        $kq = mysql_query($sql) or die(mysql_error());
        return $kq;
        
    }
    
    public function DelTrinhDo($madm_td){
            $sql="  DELETE FROM dm_trinhdo WHERE madm_td='".$madm_td."'";
            $kq = mysql_query($sql) or die(mysql_error());
            return $kq;
    }public function getItemTrinhdo($madm_td){
        $sql="select * from dm_trinhdo where madm_td='".$madm_td."'";
        $ex=mysql_query($sql);
        if (mysql_num_rows($ex)){
            $kq =mysql_fetch_array($ex);
            }
        return $kq;
    }
//Phân trang
    public function getPages($where,$offset,$limit){
      $result = mysql_query("SELECT * 
                            FROM dm_trinhdo 
                            $where 
                            LIMIT $offset, $limit");
      $arr_td= array();
      
      while($row = mysql_fetch_array($result)){
        $arr_td[] =  $this->SetDmTrinhDoBLL($row);
      }
      return $arr_td;
    }
    function paging($totalRecords,$limit,$url,$css){
		$totalPage=ceil($totalRecords/$limit);
		$pager='';
		for($i=1;$i<=$totalPage;$i++){
            $pager.="<a href='$url&p=$i' class='$css'>$i</a>";
		}
		return $pager;
	}
	function buildSearchQueryLike($keyword){    
		$dieukien="";   
		$qse=" OR ";
		$arr = tachChuoi($keyword);	
		foreach($arr as $kw){						
			$dieukien .=" madm_td LIKE '%$kw%' OR ten_td LIKE '%$kw%'";			
			$dieukien .= $qse;	
							
		}
		$cstr = strlen($qse);
		$dieukien = substr($dieukien,0,-$cstr);		
		return $dieukien;	
		
	}
    function timkiem($where){
        $sql="SELECT madm_td, ten_td, ten_vt_td FROM dm_trinhdo $where";
        $ex=mysql_query($sql);
        if($ex){
            while($row=mysql_fetch_array($ex)){
                $dstk[]=$this->SetDmTrinhDoBLL($row);
            }
        }
        return $dstk;
    }
//xoa tat ca
    public function deleteAll(){
        $sql="DELETE FROM dm_trinhdo";
        $result = mysql_query($sql) or die (mysql_error());
        return $result;
    }
}
?>